const mongoose = require('mongoose')
const {resolve} = require('path')
const config = require('../config')
const fs = require('fs')
const models= resolve(__dirname,'../database/schema')

fs.readdirSync(models)
.filter(file=>~file.search(/^[^\.].*js$/))
.forEach(file=>require(resolve(models,file)))

module.exports=()=>{
    mongoose.set('debug',true)

    mongoose.connect(config.db)

    mongoose.connection.on('error',()=>{
        console.errors('mongoose error')
    })
    mongoose.connection.on('open',async ()=>{
        console.log('mongoose open')
    })
}
